﻿'Imports ThoughtWorks.QRCode.Codec
'Imports ThoughtWorks.QRCode.Codec.Data
'Imports ThoughtWorks.QRCode.Codec.Util
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Text
Imports System.Xml.XPath
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Drawing


Public Class Utils
    Public Shared Function totalALetrasCFDI(ByVal total As Double, ByVal tipoMoneda As String) As String
        Try
            Dim cs_letras As Numalet = New Numalet()
            Dim tipo As String = ""
            If (tipoMoneda = "MXN" Or tipoMoneda = "MXP") Then
                tipo = "PESOS"
            ElseIf (tipoMoneda = "EUR") Then
                tipo = "EUROS"
            ElseIf (tipoMoneda = "DLS" Or tipoMoneda = "USD") Then
                tipo = "DOLARES"
            Else
                tipo = "PESOS"
            End If
            'let.CultureInfo = new CultureInfo(cboCultureInfo.SelectedItem.ToString());
            cs_letras.CultureInfo = New System.Globalization.CultureInfo("es-MX")
            cs_letras.SeparadorDecimalSalida = tipo
            cs_letras.LetraCapital = True
            cs_letras.MascaraSalidaDecimal = "00'/100' " & tipoMoneda
            cs_letras.Decimales = 2
            cs_letras.ApocoparUnoParteEntera = True
            Dim totLetras As String = cs_letras.ToCustomCardinal(total.ToString())
            Return totLetras.ToUpper()
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Public Shared Function totalALetras(ByVal total As Double) As String
        Try
            Dim cs_letras As Numalet = New Numalet()
            'let.CultureInfo = new CultureInfo(cboCultureInfo.SelectedItem.ToString());
            cs_letras.CultureInfo = New System.Globalization.CultureInfo("es-MX")
            cs_letras.SeparadorDecimalSalida = "PESOS"
            cs_letras.LetraCapital = True
            cs_letras.MascaraSalidaDecimal = "00'/100' M.N."
            cs_letras.Decimales = 2
            cs_letras.ApocoparUnoParteEntera = True
            Dim totLetras As String = cs_letras.ToCustomCardinal(total.ToString())
            Return totLetras.ToUpper()
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Public Shared Function totalALetrasIngles(ByVal total As Double) As String
        Try
            Dim cs_letras As NumaletIn = New NumaletIn()
            'let.CultureInfo = new CultureInfo(cboCultureInfo.SelectedItem.ToString());
            cs_letras.CultureInfo = New System.Globalization.CultureInfo("en-US")
            cs_letras.SeparadorDecimalSalida = "DOLLARS"
            cs_letras.LetraCapital = True
            cs_letras.MascaraSalidaDecimal = "00'/100' U.S."
            cs_letras.Decimales = 2
            cs_letras.ApocoparUnoParteEntera = True
            Dim totLetras As String = cs_letras.ToCustomCardinal(total.ToString())
            Return totLetras.ToUpper()
        Catch ex As Exception
            Return ""
        End Try
    End Function

    'Public Shared Function formaCBBParaCfdi(ByVal pRfcEmi As String, ByVal pRfcRecept As String, ByVal pTotal As Decimal, ByVal pUuid As String) As Byte()
    '    Try
    '        Dim strCBB As String = "?re=" & pRfcEmi & "&rr=" & pRfcRecept & "&tt=" & String.Format("{0:0000000000.000000}", pTotal) & "&id=" & pUuid
    '        Dim qrCodeEnc As QRCodeEncoder = New QRCodeEncoder()
    '        Dim imagenCBB As Image
    '        Dim arrImage() As Byte = Nothing
    '        Dim ms As New IO.MemoryStream
    '        qrCodeEnc.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE
    '        qrCodeEnc.QRCodeScale = 4
    '        qrCodeEnc.QRCodeVersion = 7
    '        qrCodeEnc.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M
    '        imagenCBB = qrCodeEnc.Encode(strCBB)
    '        imagenCBB.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
    '        arrImage = ms.GetBuffer
    '        ms.Close()
    '        ms.Dispose()
    '        ms = Nothing
    '        imagenCBB.Dispose()
    '        imagenCBB = Nothing
    '        qrCodeEnc = Nothing
    '        strCBB = Nothing
    '        formaCBBParaCfdi = arrImage
    '        arrImage = Nothing
    '    Catch ex As Exception
    '        formaCBBParaCfdi = Nothing
    '    End Try
    'End Function

    Public Shared Function getCadenaOriginalFromCFDI(ByVal pXml As XmlNode, ByVal ruta As String, ByRef pResultMsg As String) As String
        pResultMsg = ""
        Try
            'Application.StartupPath() & "\XsltSat\cadenaoriginal_TFD_1_0.xslt"
            If (File.Exists(ruta)) Then
                Dim transformer As XslCompiledTransform
                Dim utf8 As Encoding = Encoding.UTF8
                Dim strwriter As System.IO.StringWriter = New System.IO.StringWriter()
                transformer = New XslCompiledTransform()
                transformer.Load(New XPathDocument(ruta))
                transformer.Transform(pXml, Nothing, strwriter)
                Return strwriter.ToString()
                strwriter = Nothing
                utf8 = Nothing
                transformer = Nothing
            Else
                pResultMsg &= "No se encontró el archivo de inicio del Xslt." & vbCrLf
                Return "2"
            End If
        Catch ex As Exception
            pResultMsg &= "Error al formar la Cadena Original del Timbre: " & ex.Message & vbCrLf
            Return ""
        End Try
    End Function

    'Public Shared Sub AbrirPDF(ByVal nombreComprobante As String, ByVal bznEntrada As String)
    '    Try
    '        If (nombreComprobante <> "") Then
    '            If (Directory.Exists(bznEntrada)) Then
    '                For Each archivo As String In My.Computer.FileSystem.GetFiles(bznEntrada, _
    '                                                                              FileIO.SearchOption.SearchAllSubDirectories, _
    '                                                                              "*.pdf")
    '                    Path.GetFileName(archivo)
    '                    If (archivo.Contains(nombreComprobante)) Then
    '                        System.Diagnostics.Process.Start(archivo)
    '                        Exit For
    '                    End If
    '                Next
    '            Else
    '                MessageBox.Show("No existe el Folder en la ruta especificada para Buzón de Entrada.", "Atención", _
    '                                MessageBoxButtons.OK, MessageBoxIcon.Error)
    '            End If
    '        End If
    '    Catch ex As Exception
    '    End Try
    'End Sub

    Public Shared Function cambio(ByVal x As Integer) As String
        Dim num As String = ""
        Select Case x
            Case 1
                num = "one "
            Case 2
                num = "two "
            Case 3
                num = "three "
            Case 4
                num = "four "
            Case 5
                num = "five "
            Case 6
                num = "six "
            Case 7
                num = "seven "
            Case 8
                num = "eight "
            Case 9
                num = "nine "
            Case 10
                num = "ten "
            Case 11
                num = "eleven "
            Case 12
                num = "twelve "
            Case 13
                num = "thirteen "
            Case 14
                num = "fourteen "
            Case 15
                num = "fifteen "
            Case 16
                num = "sixteen "
            Case 17
                num = "seventeen "
            Case 18
                num = "eightteen "
            Case 19
                num = "nineteen "
            Case 20 To 29
                num = "twenty " & cambio(x - 20)
            Case 30 To 39
                num = "thirty " & cambio(x - 30)
            Case 40 To 49
                num = "fourty " & cambio(x - 40)
            Case 50 To 59
                num = "fifty " & cambio(x - 50)
            Case 60 To 69
                num = "sixty " & cambio(x - 60)
            Case 70 To 79
                num = "seventy " & cambio(x - 70)
            Case 80 To 89
                num = "eighty " & cambio(x - 80)
            Case 90 To 99
                num = "ninety " & cambio(x - 90)
            Case 100 To 199
                num = "One hundred " & cambio(x - 100)
            Case 200 To 299
                num = "Two hundred " & cambio(x - 200)
            Case 300 To 399
                num = "three hundred " & cambio(x - 300)
            Case 400
            Case 500
            Case 600
            Case 700
            Case 800
            Case 900 To 999
                num = "nine hundred " & cambio(x - 900)
            Case 1000 To 1999
                num = "One thousand  " & cambio(x - 1000)
            Case 2000 To 2999
                num = "two thousand  " & cambio(x - 2000)
        End Select
        Return num
    End Function

    Public Shared Function Dolares(ByVal tyCantidad As Decimal) As String
        Dim lyCantidad As Decimal, lyCentavos As Decimal, lnDigito As Byte, lnPrimerDigito As Byte, lnSegundoDigito As Byte, lnTercerDigito As Byte, lcBloque As String, lnNumeroBloques As Byte, lnBloqueCero
        tyCantidad = Decimal.Round(tyCantidad, 2)
        lyCantidad = Int(tyCantidad)
        lyCentavos = (tyCantidad - lyCantidad) * 100
        Dim laUnidades As String() = {"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN", "TWENTY", "TWENTY ONE", "TWENTY TWO", "TWENTY THREE", "TWENTY FOUR", "TWENTY FIVE", "TWENTY SIX", "TWENTY SEVEN", "TWENTY EIGHT", "TWENTY NINE"}
        Dim laDecenas As String() = {"TEEN", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY", "SEVENTY", "EIGHTY", "NINETY"}
        Dim laCentenas As String() = {"ONE HUNDRED", "TWO HUNDRED", "THREE HUNDRED", "FOUR HUNDRED", "FIVE HUNDRED", "SIX HUNDRED", "SEVEN HUNDRED", "EIGHT HUNDRED", "NINE HUNDRED"}
        lnNumeroBloques = 1
        Do
            lnPrimerDigito = 0
            lnSegundoDigito = 0
            lnTercerDigito = 0
            lcBloque = ""
            lnBloqueCero = 0
            For i = 1 To 3
                lnDigito = lyCantidad Mod 10
                If lnDigito <> 0 Then
                    Select Case i
                        Case 1
                            lcBloque = " " & laUnidades(lnDigito - 1)
                            lnPrimerDigito = lnDigito
                        Case 2
                            If lnDigito <= 2 Then
                                lcBloque = " " & laUnidades((lnDigito * 10) + lnPrimerDigito - 1)
                            Else
                                lcBloque = " " & laDecenas(lnDigito - 1) & lcBloque
                            End If
                            lnSegundoDigito = lnDigito
                        Case 3
                            lcBloque = " " & IIf(lnDigito = 1 And lnPrimerDigito = 0 And lnSegundoDigito = 0, "CIEN", laCentenas(lnDigito - 1)) & lcBloque
                            lnTercerDigito = lnDigito
                    End Select
                Else
                    lnBloqueCero = lnBloqueCero + 1
                End If
                lyCantidad = Int(lyCantidad / 10)
                If lyCantidad = 0 Then
                    Exit For
                End If
            Next i
            Select Case lnNumeroBloques
                Case 1
                    Dolares = lcBloque
                Case 2
                    Dolares = lcBloque & IIf(lnBloqueCero = 3, DBNull.Value, " THOUSAND") & Dolares
                Case 3
                    Dolares = lcBloque & IIf(lnPrimerDigito = 1 And lnSegundoDigito = 0 And lnTercerDigito = 0, " MILLON", " MILLONES") & Dolares
            End Select
            lnNumeroBloques = lnNumeroBloques + 1
        Loop Until lyCantidad = 0
        Dolares = "(=" & Dolares & IIf(tyCantidad > 1, " DOLLARS ", " DOLLAR ") & Format(Str(lyCentavos), "00") & "/100 USD =)"
    End Function
End Class
